<?php
class Borrower extends Controller
{
	private $tbl = 'tbl_borrowers';
	private  $tbl_borrower_qty = 'tbl_borrower_quantities';
	private $tbl_member = 'tbl_members';

	function Borrower()
	{
		parent::Controller();
		$this->load->model('borrower_model');
		$this->load->helper('text');
	}

	function index()
	{
      	$data['total_borrower'] = $this->borrower_model->total_borrower();
        create_pagination($data['total_borrower'], 3, config_item('per_page'));
        $data['borrowers'] = $this->borrower_model->get_borrower(config_item('per_page'), $this->uri->segment(3));
		$data['page_title']	= 'Manage Publication Borrower';
		$data['page'] = 'borrower_list_v';
		$this->load->view('main', $data);
	}

	function add()
	{
       	$this->load->model('member_model');
		if($this->input->post('submit_borrower'))
		{
			$this->add_edit();
		}

		$data['page_title']	 = 'Add New Borrower';
		$data['page'] = 'borrower_add_v';
        $data['member_list'] = $this->member_model->get_member_list();
		$this->load->view('main', $data);
	}

	function edit($publication_id)
	{
		if($this->input->post('submit_borrower'))
		{
			$this->add_edit($publication_id);
		}
		//$data['categories'] = $this->general_db_model->getAll($this->tbl, 'locations_name', 'parent_id = 0');
		$data['_borrower'] = $this->general_db_model->getById($this->tbl, 'id', $publication_id);

		$this->load->model('member_model');
		$data['member_list'] = $this->member_model->get_member_list();

		$data['page_title']	 = 'Update Publication Borrower';
		$data['page'] = 'borrower_add_v';
		$this->load->view('main', $data);
	}

	function add_edit($id = NULL)
	{
	    $user_type = $this->input->post('user_type');
        switch ($user_type)
		{
	        case "member":
	            $data['member_id'] = $this->input->post('member_id');
                $member_detail = $this->general_db_model->getById($this->tbl_member, 'id', $data['member_id']);
                $data['first_name'] = $member_detail->first_name;
                $data['last_name'] = $member_detail->last_name;
                $data['email'] = $member_detail->email;
                $data['address1'] = $member_detail->address1;
                $data['address2'] = $member_detail->address2;
                $data['phone1'] = $member_detail->phone1;
                $data['phone2'] = $member_detail->phone2;
                break;
			default:
		        $data['member_id'] = 0;
	            $data['first_name'] = $this->input->post('first_name');
                $data['last_name'] = $this->input->post('last_name');
	            $data['email'] = $this->input->post('email');
                $data['address1'] = $this->input->post('address1');
                $data['address2'] = $this->input->post('address2');
                $data['phone1'] = $this->input->post('phone1');
                $data['phone2'] = $this->input->post('phone2');
        }

		//debug_array($_POST);
		//die();
		if($id)
	    {
		 	$this->general_db_model->update($this->tbl, $data, 'id = '.$id);
            $borrower_id = $id;
			$this->session->set_flashdata('success_message', "Borrower - Successfully updated.");
        }
        else
        {
		 	$this->general_db_model->insert($this->tbl, $data);
            $borrower_id = $this->db->insert_id();
		 	$this->session->set_flashdata('success_message', "Borrower - Successfully added.");
        }

		$borrow = $this->input->post('borrow');

		if($borrow)
		{
			foreach($borrow as $key=>$val)
			{
				//debug_array($val);
				$borrow_id = $val['borrow_id'];


				$prev_record = $this->general_db_model->getById($this->tbl_borrower_qty, 'id', $borrow_id);

				$return_qty = $val['return_qty'];
				$update_return_qty = $return_qty - $prev_record->return_qty;

				if($update_return_qty == 0)
					$update_return_qty = true;

				$refundable_qty = $val['refundable_qty'];
				$update_qty = $refundable_qty - $prev_record->refundable_qty;

				if($return_qty == $refundable_qty)
					$val['status'] = 1;
				else
				{
					$val['status'] = 0;
				}

				unset($val['borrow_id']);
				$val['last_return'] = date('Y-m-d');
				$this->general_db_model->update($this->tbl_borrower_qty, $val, 'id = '.$borrow_id);

				echo $update_qty.'&nbsp;'.$update_return_qty."<br>";
				$this->borrower_model->update_quantity($prev_record->publication_id, $prev_record->type, $update_qty, $update_return_qty);
				echo $this->db->last_query()."<br>";



			}

		}
		//die();

        $publication_list = $this->input->post('publication_list');

		if($publication_list)
		{
			$issue_date  = date('Y-m-d');

			$i=0;

			foreach($publication_list as $publication_id)
			{

				$refundable_en_quantity = $this->input->post('refundable_en_quantity');
				$non_refundable_en_quantity = $this->input->post('non_refundable_en_quantity');
				$refundable_en_days_list = $this->input->post('refundable_en_days');

				if((is_numeric($refundable_en_quantity[$i]) && $refundable_en_quantity[$i] > 0) || (is_numeric($non_refundable_en_quantity[$i]) && $non_refundable_en_quantity[$i] > 0))
				{
					if(empty($refundable_en_quantity[$i]))
					{
						$dataEnQuantity['status'] = 1;
					}
					else
					{
						$dataEnQuantity['status'] = 0;
					}


					$dataEnQuantity['borrower_id'] = $borrower_id;
					$dataEnQuantity['publication_id'] = $publication_id;
					$dataEnQuantity['type'] = 'en';
					$dataEnQuantity['refundable_qty'] = (is_numeric($refundable_en_quantity[$i]) && $refundable_en_quantity[$i] > 0) ?$refundable_en_quantity[$i] : 0;
					$dataEnQuantity['non_refundable_qty'] = (is_numeric($non_refundable_en_quantity[$i]) && $non_refundable_en_quantity[$i] > 0) ?$non_refundable_en_quantity[$i] : 0;
					$dataEnQuantity['issue_date'] = $issue_date;

					//mk return date
					if($dataEnQuantity['refundable_qty'] > 0)
					{
						$refundable_en_days = (is_numeric($refundable_en_days_list[$i]) && $refundable_en_days_list[$i] != "") ? $refundable_en_days_list[$i] : 15;
						$return_date_format = mktime(0, 0, 0, date('m'), date('d')+$refundable_en_days, date('Y'));
						$dataEnQuantity['return_date'] = date('Y-m-d',$return_date_format);
					}

					$this->general_db_model->insert($this->tbl_borrower_qty, $dataEnQuantity);

					if($dataEnQuantity['refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'en', $dataEnQuantity['refundable_qty'], true);

					if($dataEnQuantity['non_refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'en', $dataEnQuantity['non_refundable_qty']);

				}

				$refundable_np_quantity = $this->input->post('refundable_np_quantity');
				$non_refundable_np_quantity = $this->input->post('non_refundable_np_quantity');
				$refundable_np_days_list = $this->input->post('refundable_np_days');

				if((is_numeric($refundable_np_quantity[$i]) && $refundable_np_quantity[$i] > 0) || (is_numeric($non_refundable_np_quantity[$i]) && $non_refundable_np_quantity[$i] > 0))
				{
					if(empty($refundable_np_quantity[$i]))
					{
						$dataNpQuantity['status'] = 1;
					}
					else
					{
						$dataNpQuantity['status'] = 0;
					}

					$dataNpQuantity['borrower_id'] = $borrower_id;
					$dataNpQuantity['publication_id'] = $publication_id;
					$dataNpQuantity['type'] = 'np';
					$dataNpQuantity['refundable_qty'] = (is_numeric($refundable_np_quantity[$i]) && $refundable_np_quantity[$i] > 0) ?$refundable_np_quantity[$i] : 0;
					$dataNpQuantity['non_refundable_qty'] = (is_numeric($non_refundable_np_quantity[$i]) && $non_refundable_np_quantity[$i] > 0) ?$non_refundable_np_quantity[$i] : 0;
					$dataNpQuantity['issue_date'] = $issue_date;

					//mk return date
					if($dataNpQuantity['refundable_qty'] > 0)
					{
						$refundable_np_days = (is_numeric($refundable_np_days_list[$i]) && $refundable_np_days_list[$i] != "") ? $refundable_np_days_list[$i] : 15;
						$return_date_format = mktime(0, 0, 0, date('m'), date('d')+$refundable_np_days, date('Y'));
						$dataNpQuantity['return_date'] = date('Y-m-d',$return_date_format);
					}

					$this->general_db_model->insert($this->tbl_borrower_qty, $dataNpQuantity);

					if($dataNpQuantity['refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'np', $dataNpQuantity['refundable_qty'], true);

					if($dataNpQuantity['non_refundable_qty'] > 0)
						$this->borrower_model->update_quantity($publication_id, 'np', $dataNpQuantity['non_refundable_qty']);

				}

				$i++;

			}
		}

        redirect(current_url());

	}







	function delete()



	{



	 	$location_id = $this->input->post('c_id');



		$this->general_db_model->delete($this->tbl, 'id = '.$location_id);



	}

        function ajax_add_borrower_form()
		{
            $this->load->model('publication_model');
            $cntNo = $this->input->post('cntNo');
            $publication_list = $this->publication_model->get_publication_list();
            $data['cntNo']	 = $cntNo;
            $data['publication_list'] = $publication_list;
            $this->load->view('borrower_ajax_add_borrower_form',$data);
        }


        function ajax_get_max_publication()
		{
		    $this->load->model('quantity_model');
		    $publication_id = $this->input->post('publication_id');
			$qty_box_en = quantity($publication_id, 'en', 'total_qty_box');
			$qty_box_np = quantity($publication_id, 'np', 'total_qty_box');
			$qty_distribution_en = quantity($publication_id, 'en', 'total_qty_distribution');
			$qty_distribution_np = quantity($publication_id, 'np', 'total_qty_distribution');
		    //$quantity_en_list = $this->quantity_model->get_quantity_by_pub_type($publication_id,'en');
		    //$quantity_np_list = $this->quantity_model->get_quantity_by_pub_type($publication_id,'np');
		    echo "~~".$qty_box_en."~~".$qty_box_np."~~".$qty_distribution_en."~~".$qty_distribution_np."~~";
		}

}







?>